<?php
function get_custom_field_posts_join($join) {
	global $wpdb, $customFields;
	return $join . "  JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key in ($customFields)) ";
}
function get_custom_field_posts_group($group) {
	global $wpdb;
	$group .= " $wpdb->posts.ID ";
	return $group;
}
function user_list_widget(){
global $customFields;
$customFields = "'wpbl_user'"; //Comma seperated 's1', 's2', 's3'
$type = 'library';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'paged' => $paged,
  'posts_per_page' => 5,
  'caller_get_posts'=> 1
);
$temp = $wp_query;  // assign orginal query to temp variable for later use   
$wp_query = null;
$wp_query = new WP_Query($args); 
//$customPosts = new WP_Query();
add_filter('posts_join', 'get_custom_field_posts_join');
add_filter('posts_groupby', 'get_custom_field_posts_group');
//$customPosts->query('showposts=5' );//Uses same parameters as query_posts
remove_filter('posts_join', 'get_custom_field_posts_join');
remove_filter('posts_groupby', 'get_custom_field_posts_group');
while ($wp_query->have_posts()) : $wp_query->the_post();
$users = get_post_custom_values("wpbl_user");

?>
<li><a href="<?php the_permalink($post->ID);?>"><?php echo $users[0]; ?></a></li>
<?php endwhile; 
}

function author_dropdown_search() {
    $get_tags = get_terms('authors', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_author" onchange="document.location.href=this.options[this.selectedIndex].value;" name="select_author">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Authors', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option value="%1$s">%2$s</option>', get_term_link($tag->slug, 'authors'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}

function edition_dropdown_search() {
    $get_tags = get_terms('editions', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_edition" onchange="document.location.href=this.options[this.selectedIndex].value;" name="select_edition">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Edition', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option value="%1$s">%2$s</option>', get_term_link($tag->slug, 'editions'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}

function publisher_dropdown_search() {
    $get_tags = get_terms('publishers', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_publisher" onchange="document.location.href=this.options[this.selectedIndex].value;" name="select_publisher">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Publishers', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option value="%1$s">%2$s</option>', get_term_link($tag->slug, 'publishers'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}

function classcat_dropdown_search() {
    $get_tags = get_terms('classcat', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_classcat" onchange="document.location.href=this.options[this.selectedIndex].value;" name="select_classcat">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Category/Class', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option value="%1$s">%2$s</option>', get_term_link($tag->slug, 'classcat'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}

function dates_dropdown_search() {
    $get_tags = get_terms('dates', 'order_by=name');
    $dropdown = '';
    $dropdown_start = '<select id="select_dates" onchange="document.location.href=this.options[this.selectedIndex].value;" name="select_dates">';
    $dropdown_start .= sprintf('<option>%s</option>', __('Select Date to Search', 'adw'));
    foreach($get_tags as $tag):
        $dropdown .= sprintf('<option value="%1$s">%2$s</option>', get_term_link($tag->slug, 'dates'), $tag->name);
    endforeach;
    $dropdown_end = '</select>';
    echo $dropdown_start.$dropdown.$dropdown_end;
}

//Search Author Widget
class author_widget extends WP_Widget {
    /** constructor */
    function author_widget() {
        parent::WP_Widget(false, $name = 'Search Authors');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . $instance['title']
                      . $after_title; ?>
                  <?php author_dropdown_search(); ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }

}
//Search Edition Widget
class edition_widget extends WP_Widget {
    /** constructor */
    function edition_widget() {
        parent::WP_Widget(false, $name = 'Search Editions');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . $instance['title']
                      . $after_title; ?>
                  <?php edition_dropdown_search(); ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }

}
//Publisher Search Widget
class publisher_widget extends WP_Widget {
    /** constructor */
    function publisher_widget() {
        parent::WP_Widget(false, $name = 'Search Publishers');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . $instance['title']
                      . $after_title; ?>
                  <?php publisher_dropdown_search(); ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }

}
//Category/Class Search Widget
class classcat_widget extends WP_Widget {
    /** constructor */
    function classcat_widget() {
        parent::WP_Widget(false, $name = 'Search Category/Class');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . $instance['title']
                      . $after_title; ?>
                  <?php classcat_dropdown_search(); ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }

}

//User Search Widget
class user_widget extends WP_Widget {
    /** constructor */
    function user_widget() {
        parent::WP_Widget(false, $name = 'Search By User');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . $instance['title']
                      . $after_title; ?>
                  <?php user_list_widget(); ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }

}

//Date Search Widget
class date_widget extends WP_Widget {
    /** constructor */
    function date_widget() {
        parent::WP_Widget(false, $name = 'Search By Listed Date');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {		
        extract( $args );
        ?>
              <?php echo $before_widget; ?>
                  <?php echo $before_title
                      . $instance['title']
                      . $after_title; ?>
                  <?php dates_dropdown_search(); ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }

}
function footer_java(){ ?>
	<script language="JavaScript" type="text/JavaScript">
function sendText(e, text)
{
  e.value = e.value + text
}
</script> 
<?php }
add_action('wp_footer','footer_java');
add_action('widgets_init', create_function('', 'return register_widget("author_widget");'));
add_action('widgets_init', create_function('', 'return register_widget("edition_widget");'));
add_action('widgets_init', create_function('', 'return register_widget("publisher_widget");'));
add_action('widgets_init', create_function('', 'return register_widget("classcat_widget");'));
//add_action('widgets_init', create_function('', 'return register_widget("user_widget");'));
add_action('widgets_init', create_function('', 'return register_widget("date_widget");'));
?>